<?php
$result = [
    'status' => 0,
    'data' => null,
    'message' => '未知错误'
];

include_once '../mysql/sql.php';
include_once '../mysql/sql-feedback.php';

if (isset($sql) && $sql) {
    $params = [
        'userid' => uniqid()
    ];

    $params['username'] = isset($_POST['username']) && $_POST['username'] != '' ? $_POST['username'] : null;
    $params['password'] = isset($_POST['password']) && $_POST['password'] != '' ? $_POST['password'] : null;
    $params['email'] = isset($_POST['email'])  && $_POST['email'] != '' ? $_POST['email'] : null;
    $params['phone'] = isset($_POST['phone'])  && $_POST['phone'] != '' ? $_POST['phone'] : null;
    $params['sex'] = isset($_POST['sex']) && $_POST['sex'] != '' ? $_POST['sex'] : null;
    $params['birthday'] = isset($_POST['birthday'])  && $_POST['birthday'] != '' ? $_POST['birthday'] : null;

    $isRegister = is_register($mysql, $params);
    if ($isRegister) {
        $result['status'] = 0;
        foreach ($isRegister as $key => $user) {
            if ($user['username'] === $params['username'] || $user['email'] === $params['username'] || $user['phone'] === $params['username']) {
                $result['data'] = $params['username'];
                $result['message'] = '用户名已注册, 注册失败';
                break;
            }
            else if ($user['username'] === $params['email'] || $user['email'] === $params['email'] || $user['phone'] === $params['email']) {
                $result['data'] = $params['email'];
                $result['message'] = '邮箱已注册, 注册失败';
                break;
            }
            else if ($user['username'] === $params['phone'] || $user['email'] === $params['phone'] || $user['phone'] === $params['phone']) {
                $result['data'] = $params['phone'];
                $result['message'] = '手机已注册, 注册失败';
                break;
            };
        };
    }
    else {
        $register = register($mysql, $params);
        if ($register === 1) {
            $initType = init_type($mysql, $params);
            if ($initType === 1) {
                $result['status'] = 1;
                $result['data'] = $params['username'];
                $result['message'] = '注册成功';
            }
            else {
                $result['status'] = 0;
                $result['data'] = $params['username'];
                $result['message'] = '注册成功, 初始化帐单类型失败';
            };
        }
        else {
            $result['status'] = 0;
            $result['data'] = false;
            $result['message'] = '注册失败';
        };
    };

    // 关闭连接
    $mysql->close();
}
else {
    $result['message'] = '数据库连接错误';
};

echo json_encode($result);

// 查询帐号是否有注册过
function is_register(&$mysql, $params) {
    $username = ''; $email = ''; $phone = '';
    if ($params['username']) {
        $username = " or (username = '" . $params['username'] . "' or email = '" . $params['username'] . "' or phone = '" . $params['username']. "')";
    };
    if ($params['email']) {
        $email = " or (username = '" . $params['email'] . "' or email = '" . $params['email'] . "' or phone = '" . $params['email']. "')";
    };
    if ($params['phone']) {
        $phone = " or (username = '" . $params['phone'] . "' or email = '" . $params['phone'] . "' or phone = '" . $params['phone']. "')";
    };

$statement = <<< EOT
select * from users
where (id = 'xxx'){$username}{$email}{$phone};
EOT;
    // echo '<hr />', $statement, '<hr />';
    return sql_feedback($statement, $mysql);
};

// 注册
function register(&$mysql, $params) {
    foreach ($params as $key => $value) {
        $params[$key] = $value ? "'" . $value . "'" : 'null';
    };
$statement = <<< EOT
insert into users
values({$params['userid']}, {$params['username']}, {$params['password']}, {$params['email']}, {$params['phone']}, {$params['sex']}, {$params['birthday']}, default);
EOT;
    // echo '<hr />', $statement, '<hr />';
    return sql_feedback($statement, $mysql);
};

// 初始化帐单类型
function init_type(&$mysql, $params) {
$statement = <<< EOT
insert into types
    values
    (null, '工资', 1, '{$params["userid"]}'),
    (null, '生活用品', -1, '{$params["userid"]}'),
    (null, '伙食', -1, '{$params["userid"]}'),
    (null, '交通', -1, '{$params["userid"]}'),
    (null, '服饰', -1, '{$params["userid"]}'),
    (null, '娱乐', -1, '{$params["userid"]}')
EOT;
    //echo '<hr />', $statement, '<hr />';
    return sql_feedback($statement, $mysql);
};
?>